package models

import (
	"fmt"
)

var merchants map[int]*MerChant

type MerChant struct {
	Id           int    //seller id
	Name         string //商户名称
	Administor   int    //商户管理员
	Tel          string //商户客服电话
	Addr         string //商户地址
	MainProducts string //主要产品
	Status       int    // 状态 商户状态 -1 未审核 0 无效  1 正常
}

func (s *MerChant) String() string {
	return fmt.Sprintf("Merchant(%s %s)", s.Name, s.Administor)
}

/*
 获取所有申请的商户
*/

func AddMc(mc *MerChant) {
	merchants[mc.Id] = mc
}

func GetALLMerchant() []*MerChant {

	if len(merchants) == 0 {
		update()
	}

	arr := make([]*MerChant, len(merchants))
	i := 0
	for _, m := range merchants {
		arr[i] = m
		i++
	}
	return arr

}

func UpdateMCStatus(id int, status int) {
	if mc, exist := merchants[id]; exist {
		mc.Status = status
		v, err := Dbm.Update(mc)
		fmt.Println("update mc staturs", id, status, v, err)
	}
}

func update() {
	merchants = make(map[int]*MerChant)
	/*查询所有的商户信息*/
	items, err := Dbm.Select(MerChant{}, `select * from merchant`)
	if err != nil {
		fmt.Println(err.Error())
	}
	for _, item := range items {
		m := item.(*MerChant)
		merchants[m.Id] = m
	}
}

func init() {
	merchants = make(map[int]*MerChant)
}
